package com.unei.guava.demo.base;

import com.google.common.base.Splitter;
import org.junit.Test;

import java.util.Map;

/**
 * Splitter是Joiner的逆向操作
 */
public class SplitterDemo {
    /**
     * a common task for programmers is to take a string with some delimiter character
     * and plit that string on the delimiter and obtain an array of the parts of the string
     */
    @Test
    public void common(){
        String testString="Monday,Tuesday,Thursday,Friday,,";
        Iterable<String> parts=Splitter.on(",").split(testString);
        for(String s:parts)
            System.out.println(s);
    }

    /**
     * Splitter.trimResults 去掉结果集Iterable每个元素的首尾的空格
     */
    @Test
    public void trimResults(){
        //Just like the Joiner class,Splitter is immutable on creation,
        //so care must be taken to not call the trimResults method after
        //creating the original Splitter class
        Splitter splitter=Splitter.on("|").trimResults();
        Iterable<String> parts=splitter.split("1| 2 | 3 ||");
        for(String s:parts)
            System.out.println(s);
    }

    /**
     * 将指定格式的字符串分割成map
     */
    @Test
    public void testSplitToMap(){
        String testString="Washington D.C=Redskins#New York City=Giants#Philadelphia=Eagles#Dallas=Cowboys";
        Splitter.MapSplitter mapSplitter=Splitter.on("#").withKeyValueSeparator("=");
        Map<String,String> testMap=mapSplitter.split(testString);
        for(String key:testMap.keySet())
            System.out.println(testMap.get(key));

    }
}
